package com.ll.service.impl;

import com.ll.dao.entity.EmailMessageHistory;
import com.ll.dao.mapper.EmailMessageHistoryMapper;
import com.ll.exception.UserException;
import com.ll.exception.UserExceptionConstants;
import com.ll.service.EmailMessageHistoryService;
import com.ll.utils.StringUtil;
import com.ll.utils.TimeZoneUtil;
import com.ll.vo.request.EmailMessageHistoryRequestVo;
import com.ll.vo.response.PrimaryIdVo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
/**
 * 邮件历史
 * @author liudawei
 *
 */
@Service
public class EmailMessageHistoryServiceImpl implements EmailMessageHistoryService {

	private static final Logger LOGGER = LoggerFactory.getLogger(EmailMessageHistoryServiceImpl.class);
			
	@Autowired
	private EmailMessageHistoryMapper emailMessageHistoryMapper;
	
	@Override
	public PrimaryIdVo saveEmailMessageHistory(EmailMessageHistoryRequestVo emailMessageHistoryRequestVo) {
		EmailMessageHistory emailMessageHistory = new EmailMessageHistory();
		try {
			DateConverter dateConverter = new DateConverter();
			dateConverter.setTimeZone(TimeZoneUtil.getDefaultTimeZone());
			dateConverter.setPatterns(new String[] { "yyyy-MM-dd" });
			ConvertUtils.register(dateConverter, Date.class);
			BeanUtils.copyProperties(emailMessageHistory, emailMessageHistoryRequestVo);
		} catch (IllegalAccessException e) {
			LOGGER.error("EmailMessageHistory对象和EmailMessageHistoryRequestVo对象在拷贝属性值的时候，抛出异常信息", e);
			throw new UserException(UserExceptionConstants.MAINTAINING, e);
		} catch (InvocationTargetException e) {
			LOGGER.error("EmailMessageHistory对象和EmailMessageHistoryRequestVo对象在拷贝属性值的时候，抛出异常信息", e);
			throw new UserException(UserExceptionConstants.MAINTAINING, e);
		}
		String id = StringUtil.createUUID();
		emailMessageHistory.setCreateOn(new Date());
		emailMessageHistory.setId(id);
		try {
			emailMessageHistoryMapper.insertEmailMessageHistory(emailMessageHistory);
			return new PrimaryIdVo(id);
		} catch (Exception e) {
			LOGGER.error("新增邮件历史信息时报错", e);
			throw new UserException(UserExceptionConstants.MAINTAINING, e);
		}
	}

	@Override
	public void deleteEmailMessageHistoryById(String id) {
		try {
			emailMessageHistoryMapper.deleteEmailMessageHistoryById(id);
		} catch (Exception e) {
			LOGGER.error("删除邮件历史信息时报错", e);
			throw new UserException(UserExceptionConstants.MAINTAINING, e);
		}
	}

	@Override
	public int deleteEmailMessageHistoryListByIds(String[] Ids) {
		try {
			return emailMessageHistoryMapper.deleteEmailMessageHistoryListByIds(Ids);
		} catch (Exception e) {
			LOGGER.error("删除邮件历史信息时报错", e);
			throw new UserException(UserExceptionConstants.MAINTAINING, e);
		}
	}


}
